﻿using System;
using System.Windows.Forms;
using AG.Editor.Core;
using AG.Editor.Windows;
using AG.Editor.Core.Metadata;

namespace AGEditor
{
    static class Program
    {
        public const string APPCODE = "RES-EDITOR";

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            try
            {
                AGTools.BaseDirectory = AppDomain.CurrentDomain.BaseDirectory;
            }
            catch (Exception ex)
            {
                AGTools.BaseDirectory = "c:\\ageditor\\";
            }
            
            AGTools.Init(AppDomain.CurrentDomain);
            AG.Editor.Core.AGEContext.Current.Settings = AGTools.Current.SettingsStore.GetSettings(Program.APPCODE);
            if (AG.Editor.Core.AGEContext.Current.Settings == null)
            {
                AG.Editor.Core.AGEContext.Current.Settings = new AG.Editor.Core.Settings.AGESettings(Program.APPCODE);
            }

            AGELaunchWindow launchWindow = new AGELaunchWindow(AG.Editor.Core.AGEContext.Current.Settings);
            if (launchWindow.ShowDialog() == DialogResult.OK)
            {
                AG.Editor.Core.AGEContext.Current.Settings.LatestEProjectPath = launchWindow.SelectedEProject.Path;
                AG.Editor.Core.AGEContext.Current.EProject = launchWindow.SelectedEProject;
            }
            else
            {
                return;
            }

            AGTProject curProjectTemplate = AGTools.Current.TProjectStore.GetTProject(AG.Editor.Core.AGEContext.Current.EProject.TPName);
            if (curProjectTemplate != null && curProjectTemplate.IsLastest(AG.Editor.Core.AGEContext.Current.EProject.TProject))
            {
                if (MessageBox.Show("当前项目对应的模板版本太低，需要更新吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
                {
                    AG.Editor.Core.AGEContext.Current.EProject.TPVersion = curProjectTemplate.Version;
                    AG.Editor.Core.AGEContext.Current.EProject.TProject = curProjectTemplate;
                }
            }

            Application.Run(new MainWindow3());
        }
    }
}
